package CollectionDemo;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class SetEquals {
    HashSet hashSet;
    //AbstractSet 重写的equals方法,判断两个集合是否
    public boolean equals(Object o) {
        if (o == this)
            return true;

        if (!(o instanceof Set))
            return false;
        Collection<?> c = (Collection<?>) o;
        if (c.size() != hashSet.size())
            return false;
        try {
            return hashSet.containsAll(c);
        } catch (ClassCastException unused)   {
            return false;
        } catch (NullPointerException unused) {
            return false;
        }
    }

    //hashcode是将所有元素的hashcode家起来
    public int hashCode() {
        int h = 0;
        Iterator i = hashSet.iterator();
        while (i.hasNext()) {
            Object obj = i.next();
            if (obj != null)
                h += obj.hashCode();
        }
        return h;
    }


}
